<?php
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
include "safe.class.php";
$onlinepay = $_G['cache']['plugin']['online_alipay']['onlinepay'];
$postname = $_G['cache']['plugin']['online_alipay']['postname'];
$count = $_G['cache']['plugin']['online_alipay']['count'];
$membercount = $_G['cache']['plugin']['online_alipay']['membercount'];
if(empty($onlinepay)) {
	$onlinepay = '担保交易';
}
if(empty($postname)) {
	$postname = '申请担保交易';
}
define(POSTNAME,"$postname");
define(ONLINEPAY,"$onlinepay");
class threadplugin_online_alipay extends safe_weitao_plugin_online_alipay{

	var $name = ONLINEPAY;
	var $iconfile = 'image/alipay.gif';
	var $buttontext = POSTNAME;
	
	function newthread($fid) {
		global $_G;
		$discuzpay = $_G['cache']['plugin']['online_alipay']['discuzpay'];
		$freepay = $_G['cache']['plugin']['online_alipay']['freepay'];
		$count = $_G['cache']['plugin']['online_alipay']['count'];
		$membercount = $_G['cache']['plugin']['online_alipay']['membercount'];
		$countname = $_G['cache']['plugin']['online_alipay']['countname'];
		$extend = $_G['cache']['plugin']['online_alipay']['extend'];
		$extend = unserialize($extend);
		if(in_array(1,$extend)) {
			$seltion = "<option value='alipay'>线下现金交易</option>";
		}
		if(in_array(2,$extend)) {
			$seltion .= "<option value='membercount'>论坛积分交易</option>";
		}
		if($membercount == 1) {
			$countform = "
				请选择交易类型:
					<select name='choosepayment' id='choosepayment_plugin' width='100px;'>
						$seltion
					</select>
			";
		}
		include template("online_alipay:newthread");
		return $return;
	}

	function newthread_submit($fid) {
		global $_G;
	}

	function newthread_submit_end($fid, $tid) {
		global $_G;
		$credit = $_G['cache']['plugin']['online_alipay']['count'];
		$discuzpay = $_G['cache']['plugin']['online_alipay']['discuzpay'];
		$freepay = $_G['cache']['plugin']['online_alipay']['freepay'];
		$common_member = DB::table("common_member");
		$count = DB::table("common_member_count");
		$name = $this->getparameter('name',0,0);
		$act = $this->getparameter('act',0,0);
		$price = $this->getparameter('price',0,1);
		$qq = $this->getparameter('qq',0,1);
		$discuz = $this->getparameter('discuz',0,0);
		$phone = $this->getparameter('phone',0,0);
		$name_cash = $this->getparameter('name_cash',0,0);
		$act_cash = $this->getparameter('act_cash',0,0);
		$price_cash = $this->getparameter('price_cash',0,1);
		$qq_cash = $this->getparameter('qq_cash',0,1);
		$discuz_cash = $this->getparameter('discuz_cash',0,0);
		$phone_cash = $this->getparameter('phone_cash',0,1);
		$choosepayment = $this->getparameter('choosepayment',0,0);
		$thread_guarantee =  DB::table("thread_guarantee");
		if($choosepayment == 'membercount') {
			if($act == 'buyer') {
				$buyer = $_G['username'];
				$seller = $name;
			} else {
				$seller = $_G['username'];
				$buyer = $name;
			}
			if($price > $freepay) {
				$myprice = $price*$discuzpay/100;
				if($discuz == 's') {
					$sellerprice = $price-$myprice;
				} else {
					$sellerprice = $price;
					$price = $price+$myprice;
				}
			} else {
				$myprice = 0;
			}
			$query = DB::query("SELECT `extcredits$credit` FROM `$count` WHERE `uid` = '".$_G['uid']."'");
			while($user_count = mysql_fetch_array($query)) {
				$extcredits = $user_count[0];
			}
			$common_member = DB::table("common_member");
			$query = DB::query("SELECT *FROM `$common_member` WHERE `username` = '".$buyer."'");
			while($getid = mysql_fetch_array($query)) {$my_seller_uid = $getid[0];}
			updatemembercount($my_seller_uid,array("extcredits$credit" => "-$price"));
			$threadtype = 1;
			if($_G['username'] == $seller) {
				$query = DB::query("INSERT INTO `$thread_guarantee` VALUES(null,'$tid','$seller','$buyer','$qq','$phone','0','$price','$sellerprice','$myprice','0','$threadtype')");
			} else {
				$query = DB::query("INSERT INTO `$thread_guarantee` VALUES(null,'$tid','$seller','$buyer','$qq','$phone','1','$price','$sellerprice','$myprice','0','$threadtype')");
			}
		} else {
			if($act_cash == 'buyer') {
				$buyer = $_G['username'];
				$seller = $name_cash;
			} else {
				$seller = $_G['username'];
				$buyer = $name_cash;
			}
			if($price_cash > $freepay) {
				$myprice = $price_cash*$discuzpay/100;
				if($discuz == 's') {
					$sellerprice = $price_cash-$myprice;
				} else {
					$sellerprice = $price_cash;
					$price_cash = $price_cash+$myprice;
				}
			} else {
				$myprice = 0;
			}
			
			if($choosepayment == 'auth_pay') {
				$threadtype = 2;
			} else {
				$threadtype = 0;
			}
			if($_G['username'] == $seller) {
				$query = DB::query("INSERT INTO `$thread_guarantee` VALUES(null,'$tid','$seller','$buyer','$qq_cash','$phone_cash','0','$price_cash','$sellerprice','$myprice','0','$threadtype')");
			} else {
				$query = DB::query("INSERT INTO `$thread_guarantee` VALUES(null,'$tid','$seller','$buyer','$qq_cash','$phone_cash','1','$price_cash','$sellerprice','$myprice','0','$threadtype')");
			}
		}
		if($choosepayment == 'auth_pay') {
			if($_G['username'] == $buyer) {
				alipays($price_cash,$myprice,$tid);
			}
		}
		
		if($query) {
			showmessage("交易发布成功，请等待另一方审核!","plugin.php?id=online_alipay:index");
		}
	}

	function editpost($fid, $tid) {
		showmessage("担保交易帖子不可以进行编辑!");
	}

	function editpost_submit($fid, $tid) {
		showmessage("担保交易帖子不可以进行编辑!");
	}

	function editpost_submit_end($fid, $tid) {
		showmessage("担保交易帖子不可以进行编辑!");
	}

	function newreply_submit_end($fid, $tid) {
		$returnthread = $_G['cache']['plugin']['online_alipay']['returnthread'];
		if(!$returnthread) {
			showmessage("对不起,您不能回复该帖子");
		}
	}

	function viewthread($tid) {
		global $_G;
		$thread_guarantee =  DB::table("thread_guarantee");
		$onlypay = $_G['cache']['plugin']['online_alipay']['onlypay'];
		$manageid = $_G['cache']['plugin']['online_alipay']['manage'];
		$query = DB::query("SELECT * FROM `$thread_guarantee` WHERE `fid`='$tid'");
		if(mysql_fetch_array($query)) {
			$query = DB::query("SELECT * FROM `$thread_guarantee` WHERE `fid`='$tid'");
			while($fetch = mysql_fetch_array($query)) {
				$threadtype = $fetch['threadtype'];
				$buyername = $fetch['buyername'];
				$sellername = $fetch['sellername'];
			}
			$query = DB::query("SELECT * FROM `".DB::table("thread_alipayorder")."` WHERE `threadid`='$tid'");
			while($fetch = mysql_fetch_array($query)) {
				$status = $fetch['status'];
				$price = $fetch['totle'];
				$orderid = $fetch['orderid'];
			}
			$time = time();
			$return_url = $_G['siteurl'].'source/plugin/online_alipay/recharge.php'; 
			$href = $Oauth_pay."?id=auth_pay:pay&sign=$md5&price=$price&return_url=$return_url&orderid=$orderid&sign_type=md5";
			if($status == 1) {
				$status_lang = "<a href='$href'>等待支付</a>";
			} else if($status == 2){
				$status_lang = "支付成功";
			} else if($status == 3) {
				$status_lang = "等待提现";
			} else if($status == 4) {
				$status_lang = "提现成功";
			} else {
				$status_lang = "状态错误";
			}
			if($onlypay) {
				if($_G['username'] == $buyername || $_G['username'] == $sellername) {
					$useraction = $this->showthread($tid);
                                        if(empty($useraction)) {
                                                     $useraction = '当前状态无操作';
                                        }
					return $this->showtables($tid,$useraction,$add);
				} else {
					if($_G['groupid'] != $manageid) {
						showmessage("只有交易双方可以查看!");
					}
				}
			} else {
				$useraction = $this->showthread($tid);
                                if(empty($useraction)) {
                                             $useraction = '当前状态无操作';
                                }
				return $this->showtables($tid,$useraction,$add);
			}
		} else {
			showmessage("对不起,您要查找的主题不存在!");
		}
	}
	function showthread($tid) {
		global $_G;
		$manage = $_G['cache']['plugin']['online_alipay']['manage'];
		$manage = unserialize($manage);
		$thread_guarantee =  DB::table("thread_guarantee");
		$query = DB::query("SELECT * FROM `$thread_guarantee` WHERE `fid`='$tid'");
		while($list = mysql_fetch_array($query)) {
			$post = $list[6];
			$sellername = $list[2];
			$buyername = $list[3];
			$status = $list[10];
		}
		if($status == 0) {
			$href = "'plugin.php?id=online_alipay:cpmsg&tid=$tid&status=0'";
			if($post == 0) { //seller
				if($_G['username'] == $buyername) {
					return '<input type="button" value="同意交易" onclick="location.href='.$href.'" class="pn">';
				} else if($_G['username'] == $sellername) {
					return "<span>请等待对方审核</span>";
				}
			} else { // buyer
				if($_G['username'] == $sellername) {
					return '<input type="button" value="同意交易" onclick="location.href='.$href.'" class="pn">';
				} else if($_G['username'] == $buyername) {
					return "<span>请等待对方审核</span>";
				}
			}
		} else if ($status == 1){
			return "<span>请联系管理员，请管理员在后台进行审批</span>";
		} else if ($status == 2) {
			if($_G['username'] == $buyername) {
				$s = "<span>请等待卖家确认完成</span>";
			} else if($_G['username'] == $sellername) {
				$href = "'plugin.php?id=online_alipay:cpmsg&tid=$tid&status=2'";
				$s = '<input type="button" value="完成工作" onclick="location.href='.$href.'" class="pn">';
			}
			return $s;
		} else if ($status == 3) {
			if($_G['username'] == $buyername) {
				$href = "'plugin.php?id=online_alipay:cpmsg&tid=$tid&status=3'";
				$s = '<input type="button" value="确认验收" onclick="location.href='.$href.'" class="pn">';
			}else if($_G['username'] == $sellername){
				$s = "<span>请等待买家验收工作!</span>";
			}
			return $s;
		} else if ($status == 4) {
			return "<span>该笔交易已完成</span>";
		} else if ($status == 5) {
			if(in_array($_G['groupid'],$manage)) {
				return "<a href='plugin.php?id=online_alipay:cpmsg&tid=$tid&status=unfreeze'>订单已冻结，点击这里解除冻结</a>";
			} else {
				showmessage("订单冻结中");
			}
		}
	}
	
	function showtables($tid,$action,$add) {
		global $_G;
		$manage = $_G['cache']['plugin']['online_alipay']['manage'];
		$manage = unserialize($manage);
		$thread_guarantee =  DB::table("thread_guarantee");
		$query = DB::query("SELECT * FROM `$thread_guarantee` WHERE `fid` = '$tid'");
		while($threadlist = mysql_fetch_array($query)) {
			$sellername = $threadlist['sellername'];
			$buyername = $threadlist['buyername'];
			$price = $threadlist['price'];
			$qq = $threadlist['qq'];
			$myprice = $threadlist['myprice'];
			$phone = $threadlist['phone'];
		}
		if(!in_array($_G['groupid'],$manage)) {
			$admin = "&nbsp;&nbsp;&nbsp;&nbsp;<span style='font-weight:bold;'><a href='plugin.php?id=online_alipay:cpmsg&tid=$tid&status=freeze' style='color:red;'>冻结交易</a></span>";
		}
		$form = "
			<style>
				.plugin_onlinepay{
					width:100%;
					margin-bottom:5px;
				}
				.plugin_onlinepay td{
					border: 1px solid #CCCCCC;
					padding: 5px;
				}
				.plugin_onlinepay th{
					border: 1px solid #CCCCCC;
					padding: 5px;
				}
				.bgc{
					background:url('./source/plugin/online_alipay/image/titlebg.png');
				}
			</style>
			<table class='plugin_onlinepay'>
				<tr>
					<td class='bgc'>我的用户名:</td>
					<td>".$buyername."</td>
					<td class='bgc'>对方用户名:</td>
					<td>".$sellername."</td>
				</tr>
				<Tr>
					<td class='bgc'>联系电话:</td>
					<td>".$phone."</td>
					<td class='bgc'>交易价格:</td>
					<td>".$price."</td>
				</tr>
				<tr>
					<td class='bgc'>我的QQ:</td>
					<td><a rel='nofollow' target='_blank' href='http://wpa.qq.com/msgrd?V=1&Uin=".$qq."&Site=[Discuz!]&Menu=yes'><img border='0' src='./source/plugin/online_alipay/image/qq_big.gif'></td>
					<td class='bgc'>交易费:</td>
					<td>".$myprice."</td>
				</tr>
				<tr>
					<td class='bgc' colspan='2'>当前操作:</td>
					<td colspan='2'>".$action."$admin</td>
				</tr>
				$add
			</table>
		";
		return $form;
	}
}
?>